package com.cdtye.itps.cms.controller.equipment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.authcore.model.UserSso;
import com.cdtye.common.authcore.util.AuthHelper;
import com.cdtye.common.webcore.base.BaseController;
import com.cdtye.common.webcore.util.DateUtil;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.dao.enums.EquipmentEnum;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentClassType;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentClassTypeChildren;
import com.cdtye.itps.cms.dao.equipment.vo.EquipmentClassTypeVo;
import com.cdtye.itps.cms.service.equipment.EquipmentClassTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.cdtye.common.webcore.web.AjaxJson.error;
import static com.cdtye.common.webcore.web.AjaxJson.success;

@Api("设备类型 前端控制器")
@RestController
@RequestMapping("/equipmentClassType")
public class EquipmentClassTypeController extends BaseController {
    @Autowired
    private EquipmentClassTypeService modelService;

    @ApiOperation(value = "保存或更新", notes = "返回对象最新数据")
    @PostMapping(value = "/saveOrUpdate")
    public AjaxJson<EquipmentClassType> saveOrUpdate(@RequestBody EquipmentClassTypeVo vo){
        if (vo.getTypeName() == null || vo.getDtId() == null){
            return error("设备分类名称和C类型不能为空");
        }
        EquipmentClassType equipmentClassType = new EquipmentClassType();
        UserSso loginUser = AuthHelper.loginUser();
        Date nowDate = DateUtil.getDateTime();

        if(StringUtils.isBlank(equipmentClassType.getId())){
            equipmentClassType.setCreateBy(loginUser.getId());
            equipmentClassType.setCreateDatetime(nowDate);
            equipmentClassType.setUpdateDatetime(nowDate);
        }else{
            EquipmentClassType oldModel = modelService.getById(equipmentClassType.getId());
            if(oldModel==null){
                return error("未查询到修改对象");
            }
            bindRequestToModel(oldModel);
            equipmentClassType = oldModel;
        }
        equipmentClassType.setUpdateBy(loginUser.getId());
        equipmentClassType.setUpdateDatetime(nowDate);
        EquipmentClassType type= modelService.saveOrUpdates(equipmentClassType,vo);
        modelService.saveOrUpdate(type);
        return success(type);
    }

    @ApiOperation(value = "按ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @DeleteMapping(value = "/{id}")
    public AjaxJson deleteById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        Integer redundant = modelService.isRedundant(id);
        if(redundant!=1){
            return success("0");
        }
        EquipmentClassType byId = modelService.getById(id);
        byId.setDelFlag(EquipmentEnum.NO.getText());
        modelService.saveOrUpdate(byId);
        return success(redundant);
    }


    @ApiOperation(value = "按ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @GetMapping(value = "/getById/{id}")
    public AjaxJson getById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }

        return success(modelService.getByIds(id));
    }

    @ApiOperation(value = "列表查询")
    @GetMapping(value = "/list")
    public AjaxJson<List<EquipmentClassTypeChildren>> list(EquipmentClassTypeVo vo){
        List<EquipmentClassTypeChildren> result = modelService.list(vo);
        return success(result);
    }

    /**
     * id 查询以getById(id).typeNo 开头语的
     * name 模糊
     *
     * @param vo
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/listPage")
    public AjaxJson<IPage<Map<String,Object>>> listPage(EquipmentClassTypeVo vo){
        IPage<Map<String,Object>> result = modelService.listByPage(vo);
        return success(result);
    }


    /**
     * 获取列表
     * @return
     */
    @ApiOperation(value = "查询列表")
    @GetMapping(value = "/getList")
    public AjaxJson<List<Map<String,Object>>> getList(){
        List<Map<String,String>> result = modelService.getAll();
        return success(result);
    }
    /**
     * 获取父节点
     * @return
     */
    @ApiOperation(value = "查询父节点")
    @GetMapping(value = "/getByTypeNo")
    public AjaxJson<List<Map<String,Object>>> getByTypeNo(EquipmentClassTypeVo vo){
        List<Map<String,Object>> result = modelService.getByTypeNo(vo);
        return success(result);
    }

    /**
     * 根据typeNo获取对象
     * @return
     */
    @ApiOperation(value = "查询父节点")
    @GetMapping(value = "/getByTypeNo/{id}")
    public AjaxJson<EquipmentClassType> getByTypeNo(@PathVariable("id") String id){
        EquipmentClassTypeVo vo = new EquipmentClassTypeVo();
        vo.setTypeNo(id);
        List<Map<String, Object>> byTypeNo = modelService.getByTypeNo(vo);
        return success(byTypeNo);
    }

    /**
     * 根据typeNo获取对象
     * @return
     */
    @ApiOperation(value = "查询父节点")
    @GetMapping(value = "/getCheckById/{id}")
    public AjaxJson<EquipmentClassType> getCheckById(@PathVariable("id") String id){
        Integer redundant = modelService.isRedundant(id);
        return success(redundant);
    }
}
